module.exports = {
    save: function(a) {
        var t = this, e = a.currentTarget, d = t.data.form;
        d[e.dataset.key] = a.detail.value || "", t.setData({
            form: d
        }), t.validateFun(e.dataset);
    },
    saveItem: function(a, t, e) {
        var d = this, i = d.data.form;
        i[a.key] = t || "", d.setData({
            form: i
        }), d.validateFun(a, e);
    },
    validateFun: function(a, t) {
        var e = this;
        return (void 0 === a.required || !e._required(a)) && ((void 0 === a.pattern || !e._pattern(a)) && ((void 0 === a.custom || !e._custom(a)) && (!(void 0 !== a.async && !t && e._async(a)) && void e.setData({
            validate: e.data.validate
        }))));
    },
    _required: function(a) {
        var t = this, e = a.key, d = t.data.form[e];
        return "number" == typeof d && (d += ""), d ? t.data.validate[e + "_required"] = !1 : (t._clearItem(e, !1), 
        t.data.validate[e + "_required"] = !0, t.setData({
            validate: t.data.validate
        })), !d;
    },
    _pattern: function(a) {
        var t = this, e = a.key, d = a.pattern, i = t.data.form[e];
        if ("number" == typeof i && (i += ""), "" === i) return t.data.validate[e + "_pattern"] = !1, 
        !1;
        var n = (d = new RegExp(d)).test(i);
        return n ? t.data.validate[e + "_pattern"] = !1 : (t._clearItem(e, !1), t.data.validate[e + "_pattern"] = !0, 
        t.setData({
            validate: t.data.validate
        })), !n;
    },
    _async: function(a) {
        var t = this, e = a.key, d = a.async, i = t.data.form[e];
        return "number" == typeof i && (i += ""), t._clearItem(e, !1), t.data.validate[e + "_asyncing"] = !0, 
        t[d](e, i).then(function() {
            t.data.validate[e + "_asyncing"] = !1, t.data.validate[e + "_async"] = !1, t.setData({
                validate: t.data.validate
            });
        }, function() {
            t._clearItem(e, !1), t.data.validate[e + "_asyncing"] = !1, t.data.validate[e + "_async"] = !0, 
            t.setData({
                validate: t.data.validate
            });
        }), !1;
    },
    _custom: function(a) {
        var t = this, e = a.key, d = a.custom, i = t.data.form[e];
        "number" == typeof i && (i += "");
        var n = t[d](e, i, a);
        return n ? t.data.validate[e + "_custom"] = !1 : (t._clearItem(e, !1), t.data.validate[e + "_custom"] = !0, 
        t.setData({
            validate: t.data.validate
        })), !n;
    },
    _deleteItem: function(a) {
        var t = this, e = t.data.form, d = t.data.validate, i = Object.keys(t.data.validate);
        a && a.length && (i = i.filter(function(t, e) {
            return 0 === t.indexOf(a + "_");
        }));
        for (var n = 0; n < i.length; n++) delete d[i[n]];
        delete e[a], flag && (t.setData({
            validate: d
        }), t.setData({
            form: e
        }));
    },
    _clearItem: function(a, t) {
        var e = this, d = e.data.validate, i = Object.keys(e.data.validate);
        a && a.length && (i = i.filter(function(t, e) {
            return 0 === t.indexOf(a + "_");
        }));
        for (var n = 0; n < i.length; n++) d[i[n]] = !1;
        t && e.setData({
            validate: d
        });
    },
    _clearOther: function(a) {
        var t = this, e = t.data.validate, d = Object.keys(t.data.validate);
        a && a.length && (d = d.filter(function(t, e) {
            return -1 === t.indexOf(a + "_");
        }));
        for (var i = 0; i < d.length; i++) e[d[i]] = !1;
        t.setData({
            validate: e
        });
    },
    _update: function(a) {
        var t = this;
        return new Promise(function(e, d) {
            wx.createSelectorQuery().in(t).selectAll(".v-model").fields({
                dataset: !0
            }, function(d) {
                for (var i = a && a.length, n = 0; n < d.length; n++) {
                    var r = d[n].dataset, l = t.data.form[r.key] || "";
                    i ? a === r.key && t.saveItem(r, l, !0) : t.saveItem(r, l, !0);
                }
                e("done");
            }).exec();
        });
    },
    getValidate: function(a) {
        var t = this;
        t._update().then(function() {
            var e = !0;
            Object.keys(t.data.validate).forEach(function(a) {
                t.data.validate[a] && (e = !1);
            }), a(e);
        });
    },
    getValidateSync: function(a) {
        var t = this, e = !0, d = a && a.length;
        return Object.keys(t.data.validate).forEach(function(i) {
            d ? t.data.validate[i] && 0 === i.indexOf(a + "_") && (e = !1) : t.data.validate[i] && (e = !1);
        }), e;
    }
};